package com.ruoyi.project.qyxy.qua.domain;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.qyxy.person.domain.ZcPerson;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 资质职称人员计算临时处理类
 */
public class QuaPersonZcTemp {

    /**
     * 必须有的类型
     */
    private List<String> mustList = new ArrayList<>();

    /**
     * 非必须有的类型
     */
    private List<String> noMustList = new ArrayList<>();

    /**
     * 缺少的类型
     */
    private List<String> noHaveList = new ArrayList<>();

    /**
     * 是否需要计算类别
     */
    private Boolean needCompute = true;

    public List<String> getNoHaveList(){
        return this.noHaveList;
    }

    public List<String> getMustList() {
        return mustList;
    }

    public List<String> getNoMustList() {
        return noMustList;
    }

    /**
     * 是否需要计算
     * @return
     */
    public Boolean isNeedCompute(){
        return this.needCompute;
    }

    public QuaPersonZcTemp(String techSpty){
        if(StringUtils.isEmpty(techSpty)){
            needCompute = false;
            return;
        }
        if(techSpty.contains(",")){
            //所有类型
            String[] types = techSpty.split("[,]");
            String a = types[0];
            String b = types[1];
            String[] allMust;
            String[] allNoMust;
            if(a.contains("@")){
                allNoMust = a.split("[@]");
                allMust = b.split("[|]");
            }else{
                allNoMust = a.split("[|]");
                allMust = b.split("[@]");
            }
            mustList.addAll(Arrays.asList(allNoMust));
            noMustList.addAll(Arrays.asList(allMust));
        }else{
            if(techSpty.contains("@")){
                noMustList.addAll(Arrays.asList(techSpty.split("[@]")));
            }else{
                mustList.addAll(Arrays.asList(techSpty.split("[|]")));
            }
        }
    }

    /**
     * 计算类别是否齐全
     * @param zcPersonList
     * @return
     */
    public boolean compute(List<ZcPerson> zcPersonList){
        boolean flag = true;
        for (String must : mustList) {
            //是否有这个类别
            boolean isHave = false;
            for (ZcPerson zcPerson : zcPersonList) {
                if(zcPerson.getCertSpty().contains(must)){
                    isHave = true;
                }
            }
            if(!isHave){
                //如果没有这个类别记录
                addNoHave(must);
                flag = false;
            }
        }
        boolean isHaveOne = false;
        for (String noMust : noMustList) {
            for (ZcPerson zcPerson : zcPersonList) {
                if(zcPerson.getCertSpty().contains(noMust)){
                    isHaveOne = true;
                }
            }
        }
        if(!noHaveList.isEmpty() && !isHaveOne){
            //记录第一个类别
            addNoHave(noHaveList.get(0));
            flag = false;
        }
        return flag;
    }

    public void addNoHave(String type){
        boolean flag = true;
        for (String noHave : noHaveList) {
            if(noHave.equals(type)){
                flag = false;
            }
        }
        if(flag){
           noHaveList.add(type);
        }
    }

    /**
     * 包含资质人员 根据身份证号判断
     * @param zcPerson
     * @param zcPersonList
     * @return
     */
    private boolean containsPerson(ZcPerson zcPerson,List<ZcPerson> zcPersonList){
        boolean flag = false;
        for (ZcPerson person : zcPersonList) {
            if(zcPerson.getIdNum().equals(person.getIdNum())){
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 去除符合职称人员
     * @param zcPersonList 所有职称人员
     * @param zcPersonUse 资质职称已使用人员
     * @param useCount 使用人数
     */
    public void useZcPerson(List<ZcPerson> zcPersonList,List<ZcPerson> zcPersonUse,int useCount){
        if(!noHaveList.isEmpty()){
            useCount -= noHaveList.size();
        }
        //先在必须的职称人员轮训减1
        //減去的数量
        int mCount = polling(zcPersonList, zcPersonUse,mustList);
        //剩余 在或者里 减去一个
        int surplus = useCount - mCount;
        A:
        for (String noMust : noMustList) {
            for (int i = 0; i < zcPersonList.size(); i++) {
                ZcPerson zcPerson = zcPersonList.get(i);
                if(!StringUtils.isEmpty(zcPerson.getCertSpty())&&zcPerson.getCertSpty().contains(noMust)){
                    if(!containsPerson(zcPerson,zcPersonUse)){
                        zcPersonUse.add(zcPerson);
                    }
                    break A;
                }
            }
        }
        //随便减去剩余人数
        for (int i = 0; i < zcPersonList.size(); i++) {
            ZcPerson zcPerson = zcPersonList.get(i);
            if(!containsPerson(zcPerson,zcPersonUse)){
                zcPersonUse.add(zcPerson);
                surplus--;
            }
            if(surplus == 0){
                break;
            }
        }
    }


    private int polling(List<ZcPerson> zcPersonList, List<ZcPerson> zcPersonUse,List<String> list) {
        int count = 0;
        for (String type : list) {
            for (int i = 0; i < zcPersonList.size(); i++) {
                ZcPerson zcPerson = zcPersonList.get(i);
                if (!StringUtils.isEmpty(zcPerson.getCertSpty()) && zcPerson.getCertSpty().contains(type)) {
                    if(!containsPerson(zcPerson,zcPersonUse)){
                        zcPersonUse.add(zcPerson);
                        count++;
                        break;
                    }
                }
            }
        }
        return count;
    }
}
